<?php 
session_start();
if (isset($_POST['dt']) && isset($_POST['tiku']) && isset($_SESSION['openid'])){
    $openid=$_SESSION['openid'];
	$dt=trim($_POST['dt']);
    $tiku=$_POST['tiku'];
	include_once("../c/config.php");
	$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
	if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}

	$query=$mysqli->query("select nianji from u3 where openid='".$openid."' limit 0, 1");
	if($query->num_rows>0){
	    $getUser=$query->fetch_assoc();
	    $nianji=$getUser['nianji'];
	}
	$fileName=3;
	if ($nianji == '四年级'){
	    $fileName=4;
	}
	if ($nianji == '五年级'){
	    $fileName=5;
	}
	if ($nianji == '六年级'){
	    $fileName=6;
	}
	if (empty($nianji)){
	    exit;
	}
	
	
	$getFile='../'.$fileName.'c.txt';
	$tiDataInfo=json_decode(file_get_contents($getFile), true); //考题答案
	$tiData=$tiDataInfo['d'];
	if (empty($tiDataInfo)){
		$tiData=array();
		$sql="select id,dan from ti where nianji='".$fileName."' ";
		$query = $mysqli->query($sql);
		while ($list=$query->fetch_array()){
			$tiData[$list['id']]=array('dan'=>$list['dan']);  
		}
		file_put_contents($getFile, json_encode(array('d'=>$tiData)));
	}else{
		$tiData=$tiDataInfo['d'];
	}
 
	$dati=json_decode($dt);
	$tikuTmp=json_decode($tiku);
	$dadui=0; //回答正确的提数
	$fenshu=0; //考生分数
	foreach ($dati as $k => $v){
	    if (!empty($v)){
	        if ($tiData[$tikuTmp[$k]]['dan'] == $v){
	            $fenshu++;
	            $dadui++;
	        }
	    }
	}
 
	$mysqli->query("insert into ci3(fen,nianji,openid,dati,tiku,createtime) values(".$fenshu.",'".$nianji."','".$openid."','".$dt."','".$tiku."','".date("Y-m-d H:i:s")."') ");
	$mysqli->close();
}
echo json_encode(array('status'=>'ok'));

?>